<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="128px">

            <el-form-item label="" prop="phone">
                <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="nickName">
                <el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userPointsList">
            <el-table-column label="可用积分" align="center" prop="availablePoints" />
            <el-table-column label="手机号" align="center" prop="wxUser.phone" />
            <el-table-column label="昵称" align="center" prop="wxUser.nickName" />
            <el-table-column label="头像" align="center" prop="wxUser.avatar">
                <template slot-scope="scope">
                    <el-image style="width: 50px; height: 50px" :src="scope.row.wxUser.avatar" fit="fill" />
                </template>
            </el-table-column>
            <!-- <el-table-column label="是否订阅公众号" align="center" prop="subscribe" /> -->
            <!-- <el-table-column label="unionId" align="center" prop="wxUser.unionId" /> -->
            <el-table-column label="openid" align="center" prop="wxUser.openId" />
            <!-- <el-table-column label="用户类型" align="center" prop="wxUser.userType">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.weixin_user_type" :value="scope.row.wxUser.userType" />
                </template>
            </el-table-column> -->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit"
                        @click="handleUpdate(scope.row)">修改积分</el-button>
                    <el-button size="mini" type="text" icon="el-icon-view"
                        @click="handleRecord(scope.row.wxUserId)">积分记录</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改用户管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="操作类型" prop="type">
                    <el-select v-model="form.type" placeholder="请选择操作类型">
                        <el-option v-for="dict in optOptions" :key="dict.value" :label="dict.label"
                            :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="积分数" prop="points">
                    <el-input v-model="form.points" placeholder="请输入增加或减少的积分数" />
                </el-form-item>
                <el-form-item label="操作说明" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入操作说明" maxlength="200"
                        show-word-limit />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <el-dialog title="积分记录" :visible.sync="recordDialogVisable" width="800px" append-to-body>
            <el-table v-loading="loading" :data="recordList">
                <el-table-column label="用户" align="center" prop="userName" />
                <el-table-column label="调整类型" align="center" prop="sourceType">
                    <template slot-scope="scope">
                        <span v-if="scope.row.sourceType == 1" style="color: red;">增加</span>
                        <span v-if="scope.row.sourceType == 2" style="color: green;">减少</span>
                    </template>
                </el-table-column>
                <el-table-column label="调整积分数" align="center" prop="point">
                    <template slot-scope="scope">
                        <span v-if="scope.row.sourceType == 1" style="color: red;">+{{ scope.row.point }}</span>
                        <span v-if="scope.row.sourceType == 2" style="color: green;">-{{ scope.row.point }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="调整前积分" align="center" prop="prePoint" />
                <el-table-column label="调整后积分" align="center" prop="nextPoint" />
                <el-table-column label="调整时间" align="center" prop="createTime" />
                <el-table-column label="调整原因" align="center" prop="reason" />
            </el-table>
        </el-dialog>
    </div>
</template>

<script>
import { userList, updatePoints, recordList } from "@/api/points/record";

export default {
    name: "UserPonints",
    data () {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 用户管理表格数据
            userPointsList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                phone: null,
                nickName: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                type: [
                    { required: true, message: "操作类型不能为空", trigger: "blur" }
                ],
                points: [
                    { required: true, message: "积分不能为空", trigger: "blur" }
                ],
            },
            optOptions: [{
                value: 1,
                label: "增加"
            }, {
                value: 2,
                label: "减少"
            }],
            recordList: [],
            recordDialogVisable: false,
        };
    },
    created () {
        this.getList();
    },
    methods: {
        handleRecord (userId) {
            recordList({ userId: userId }).then(response => {
                this.recordList = response.data;
                this.recordDialogVisable = true;
            });
        },
        /** 查询用户管理列表 */
        getList () {
            this.loading = true;
            userList(this.queryParams).then(response => {
                this.userPointsList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel () {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset () {
            this.form = {
                wxUserId: null,
                points: null,
                type: null,
                remark: null,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 修改按钮操作 */
        handleUpdate (row) {
            this.reset();
            this.form.wxUserId = row.wxUserId;
            this.open = true;
            this.title = "操作用户积分";
        },
        /** 提交按钮 */
        submitForm () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    updatePoints(this.form).then(response => {
                        this.$modal.msgSuccess("修改成功");
                        this.open = false;
                        this.getList();
                    });
                }
            });
        }
    }
};
</script>
